import { _decorator, Component, EventTouch, Input, Node, UITransform, Vec3 } from 'cc';
import { GameScene } from './GameScene';
import { CatCardLoad } from './CatCardLoad';
import { Tools } from '../Tools';
import { CatCard } from './CatCard';
import { BaseCard, CardType } from './BaseCard';
import { CardLoad } from './CardLoad';
import { LocalStorageMgr } from '../Mgr/LocalStorageMgr';
const { ccclass, property } = _decorator;

@ccclass('DisCard')
/**
 * 弃置卡牌
 */
export class DisCard extends BaseCard {

    //卡牌
    @property(Node)
    public Card: Node = null;

    //是否碰到了这张牌
    private IsTouchStart: boolean = false;

    protected start(): void {
        this.node.on(Input.EventType.TOUCH_START, this.TouchCb, this);
        this.node.on(Input.EventType.TOUCH_MOVE, this.TouchCb, this);
        this.node.on(Input.EventType.TOUCH_END, this.TouchCb, this);
        this.node.on(Input.EventType.TOUCH_CANCEL, this.TouchCb, this);

        this.Card.active = false;
    }

    /**
     * 手指监听
     */
    private TouchCb(_e: EventTouch) {
        switch (_e.type) {
            case Input.EventType.TOUCH_START:
                this.IsTouchStart = true;
                break;
            case Input.EventType.TOUCH_MOVE:
                if (!this.IsTouchStart) return;

                let pos: any = _e.getUILocation();
                pos = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(pos.x, pos.y, 0));
                this.Card.setPosition(pos);

                break;
            case Input.EventType.TOUCH_CANCEL:
                this.TouchEnd();
                break;

            case Input.EventType.TOUCH_END:
                this.TouchEnd();
                break;
        }
    }

    /**
    * 手指抬起
    */
    public TouchEnd() {
        //先判断是否碰到了猫咪牌
        for (let cat of GameScene.Instance.CatCardLoad.getComponent(CatCardLoad).CatCardArray) {
            if (Tools.CheckHit(this.Card, cat) && cat.active) {

                if (cat.getComponent(CatCard).HeartNum == 1 && this.CardInfo.num == 0
                    && this.CardInfo.color == cat.getComponent(CatCard).CardInfo.color) {
                    cat.getComponent(CatCard).UpdateHeartNum(this.CardInfo);
                } else {
                    this.Card.active = false;
                    cat.getComponent(CatCard).UpdateHeartNum(this.CardInfo);
                }

                console.log("判读游戏结束", GameScene.Instance.BattleCardInfo, this.Card.active)

                //判断游戏结束
                if (GameScene.Instance.BattleCardInfo.length == 0 && !this.Card.active) {
                    LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.FAIL++;
                    LocalStorageMgr.Instance.UpdateLocalStroage();
                    GameScene.Instance.GameOver();
                    return;
                }

                //执行攻击逻辑，卡牌消失
                this.Card.setPosition(new Vec3(0, 0, 0));
                return;
            }


        }

        this.Card.setPosition(new Vec3(0, 0, 0));
    }

    /**
     * 更新卡牌样式
     */
    public UpdateCard(_cardtype: CardType) {
        super.UpdateCard(_cardtype);
        this.Card.active = true;
    }

}


